﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Resources;
using System.Globalization;

namespace LPV_POS.mh_pos
{
    public partial class mh_banhang5_MenhGia_ThanhToan : DevExpress.XtraEditors.XtraForm
    {
        CultureInfo culture;
        string str_lang = "";
        public decimal m_tongtien = 0;
        int igiatri = 0;


        #region "Muti language"
        private void SetLanguage_banhang(string cultureName)
        {
            try
            {
                culture = CultureInfo.CreateSpecificCulture(cultureName);
                ResourceManager rm = new ResourceManager("LPV_POS.Muti_language.MyResource", typeof(mh_banhang5_MenhGia_ThanhToan).Assembly);
                lbel_tencongty.Text = rm.GetString("menhgia", culture);
                btnxoa.Text = rm.GetString("clr", culture);
                bton_chon_1.Text = rm.GetString("dongy", culture);
                bton_thoat_1.Text = rm.GetString("thoat", culture);
             //   COL_NAME.Caption = rm.GetString("ban", culture);
                
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString(), "Note");
            }

        }

        #endregion

        public mh_banhang5_MenhGia_ThanhToan()
        {
            InitializeComponent();
            culture = CultureInfo.CurrentCulture;
            //string sSQL = "";
            thuvien othuvien = new thuvien();

            // -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -==
            // -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -==            

            DataTable tbMain = othuvien.SP_SelectDataTable("sp_DM_MENHGIA", new string[] { "@flag" }, new object[] { 1 });
            //gridControl1.DataSource = tbMain;            
                    
        }

        private void mh_banhang3_ChuyenBan_Load(object sender, EventArgs e)
        {
            cpublic ocpublic = new cpublic();
            str_lang = ocpublic.Registry_Get("mh_main_lang");
            if (str_lang == "")
                str_lang = "vi-VN";
            SetLanguage_banhang(str_lang);

            switch (str_lang)
            {
                case "vi-VN":
                    lbel_tienthua.Text = "Tiền thừa: 0";
                    break;
                case "en-US":
                    lbel_tienthua.Text = "Change: 0";
                    break;
            }

            
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Dispose();
        }

        private void btnThucHien_Click(object sender, EventArgs e)
        {
            if (new cpublic().IsNumeric(lblGiaTri.Text.Trim().Replace(",", "")))
            {
                //DataRow drDVT = gridView1.GetDataRow(gridView1.FocusedRowHandle);

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
    
        public int ShowForm()
        {
            this.ShowDialog();
            int.TryParse(lblGiaTri.Text.Replace(",", ""), out igiatri);
            return igiatri;
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            //DataRow drDVT = gridView1.GetDataRow(gridView1.FocusedRowHandle);
            //int itien = 0;
            //int.TryParse(drDVT["GIA_TRI1"] + "", out itien);
            //lblGiaTri.Text = itien.ToString("N0");
        }

        private void btnCong_Click(object sender, EventArgs e)
        {
            lblGiaTri.Text = "0";
        }

        private void lblTien_Click(object sender, EventArgs e)
        {
            int itien = 0;
            object tien = ((System.Windows.Forms.Control)sender).Tag;// gridView1.GetFocusedValue();
            int.TryParse(tien + "", out itien);

            if (new cpublic().IsNumeric(lblGiaTri.Text.Replace(",", "")))
            {
                int itien1 = 0;
                int.TryParse(lblGiaTri.Text.Replace(",", ""), out itien1);
                //

                lblGiaTri.Text = (itien + itien1).ToString("N0");
            }
            else
            {
                if (lblGiaTri.Text.Trim().EndsWith(bton_X.Text))
                    lblGiaTri.Text += "" + itien.ToString("N0");
                else
                    lblGiaTri.Text += " + " + itien.ToString("N0");
            }
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            lblGiaTri.Text = "0";
        }

        private void bton_1_Click(object sender, EventArgs e)
        {
            if (lblGiaTri.Text == "0")
                lblGiaTri.Text = ((System.Windows.Forms.Control)sender).Text;
            else
            {
                lblGiaTri.Text += ((System.Windows.Forms.Control)sender).Text;
                if (new cpublic().IsNumeric(lblGiaTri.Text.Replace(",", "")))
                {
                    int itien = 0;

                    int.TryParse(lblGiaTri.Text.Replace(",", ""), out itien);
                    lblGiaTri.Text = itien.ToString("N0");
                }
            }
        }

        private void bton_bang_Click(object sender, EventArgs e)
        {
            try
            {
                string sSQL = "SELECT " + lblGiaTri.Text.Replace(",", "").Replace("X", "*").Replace("x", "*");
                DataTable tb = new thuvien().SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL"}, new string[] { sSQL});
                if (tb.Rows.Count > 0)
                {
                    int itien = 0;
                    object stien = tb.Rows[0][0];
                    int.TryParse(stien + "", out itien);
                    lblGiaTri.Text = itien.ToString("N0");
                }
            }
            catch (Exception ex)
            { 
            
            }
        }

        private void bton_X_Click(object sender, EventArgs e)
        {
            if (!lblGiaTri.Text.Trim().EndsWith(bton_X.Text))
            {
                string svalue = lblGiaTri.Text.Trim() + " " + ((System.Windows.Forms.Control)sender).Text + " ";
                lblGiaTri.Text = svalue;
            }
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void lblGiaTri_TextChanged(object sender, EventArgs e)
        {
            if (str_lang == "")
                str_lang = "vi-VN";
            if (new cpublic().IsNumeric(lblGiaTri.Text.Replace(",", "")))
                lbel_tienthua.Text = lbel_tienthua.Text.Replace("0","") + (decimal.Parse(lblGiaTri.Text) - m_tongtien).ToString("N0");
     
        }
    }
}

